package com.hanson.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.util.PageInfo;
import com.hanson.entity.CouponsBean;
import com.hanson.entity.OrderBean;
import com.hanson.service.CouponsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("/admin/coupons")
public class CouponsController {
    @Autowired
    private CouponsService couponsService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "admin/coupons/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap modelMap, String id) {
        if (StringUtils.isNotEmpty(id)) {
            modelMap.put("data", couponsService.detail(id));
        }
        return "admin/coupons/add";
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage add(CouponsBean couponsBean) {
        ResultMessage resultMessage = new ResultMessage();
        couponsService.addCoupons(couponsBean);
        resultMessage.setMsg("操作成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage modify(CouponsBean couponsBean) {
        ResultMessage resultMessage = new ResultMessage();
        couponsService.modifyCoupons(couponsBean);
        resultMessage.setMsg("操作成功！");
        return resultMessage;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage delete(String id) {
        ResultMessage resultMessage = new ResultMessage();
        couponsService.deleteCoupons(Integer.parseInt(id));
        resultMessage.setMsg("删除成功！");
        return resultMessage;
    }

    @RequestMapping(value = "listData", method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<OrderBean> listData(int limit, int page, CouponsBean couponsBean) {
        Page<CouponsBean> pageInfo = new Page<>(page, limit);
        return new PageInfo(couponsService.listCoupons(pageInfo, couponsBean));
    }
}
